#ifndef __GW_DEVICE_H__
#define __GW_DEVICE_H__

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/portmacro.h"

#include "loragw_spi.h"
#include "loragw_gpio.h"
#include "radio.h"
#include "sx1276.h"
#include "sx1276-board.h"

#include "esp_log.h"

#define HW_VERSION  "0.0.1"
#define SW_VERSION  "0.0.1"

#define SETTINGS_NAME   "GS_pkt-fwd"

#define LIERDA_MODULE

#define SPI_DEBUG       0
#define CON_DEBUG       1
#define DEBUG_JIT       0
#define DEBUG_JIT_ERROR 1
#define DEBUG_BEACON    0

#define PIN_NUM_MISO 35
#define PIN_NUM_MOSI 16
#define PIN_NUM_CLK  0
#define PIN_NUM_CS   5

#define PIN_NUM_RF_RESET    32
#define PIN_NUM_RF_DIO0     34
// #define PIN_NUM_RF_DIO1     17
// #define PIN_NUM_RF_DIO3     5

typedef enum
{
    WORKING,
    NO_CONNECTION,
}gw_status_t;


extern int debug_printf(const char *format,...);

#define DelayMs(x) vTaskDelay(x)

#if SPI_DEBUG
    #define SPI_PRINTF(args...) printf(args)
#else
    #define SPI_PRINTF(args...)
#endif

#if CON_DEBUG
    #define CON_PRINTF(args...)   debug_printf(args)   
#else
    #define CON_PRINTF(args...)
#endif

#define MSG_DEBUG(FLAG, fmt, ...)                                                                         \
            do  {                                                                                         \
                if (FLAG)                                                                                 \
                    fprintf(stdout, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
            } while (0)

typedef struct 
{
    char ssid[36];
    char password[36];
    int wifi_timeout;
}wifi_info_t;

typedef struct 
{
    uint8_t dataServer_ip[64];//ip-string
    uint8_t dataServer_port[8];
    uint32_t rf_freq;//recv rf channel[hz]
    uint8_t sf;//[7-12]
    wifi_info_t wifi_info;
    char network_connecting_priority[4];//"weg",[w for wifi],[e for ethernet],[g for 2g],哪个在前哪个优先级高,比如weg,即优先级WIFI>ETHERNET>2G
}config_para_t;

void CON_PRINTF_ARRAY(char* buf,uint16_t size);

void switch_debug(bool enable);

bool get_debug_state(void);

uint64_t getDeviceID(void);

esp_err_t read_settings_from_nvs(void);

esp_err_t save_device_settings(void);

void get_device_settings(config_para_t* device_settings);

void set_device_settings(config_para_t* device_settings);

esp_err_t restore_factory_settings(void);

void printf_device_settings(config_para_t* device_settings,bool isPrintaBaseinfo, bool isPrintWIFIInfo);

void set_gwStatus(gw_status_t status);

gw_status_t get_gwStatus(void);

uint32_t get_GWMPTick(void);

#endif
